package cn.tedu.fresheveryday.mapper;

import cn.tedu.fresheveryday.pojo.entity.Order;
import cn.tedu.fresheveryday.pojo.dto.OrderPagerDTO;
import cn.tedu.fresheveryday.pojo.vo.OrderListVO;
import cn.tedu.fresheveryday.pojo.vo.OrderSimpleOrderIdAndDistIdVO;
import cn.tedu.fresheveryday.pojo.vo.OrderSimpleOrderIdAndUserIdVO;
import cn.tedu.fresheveryday.pojo.vo.OrderSimpleVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrderMapper {
    /**
     * 添加订单
     * @param order
     */
    void insert(Order order);

    /**
     * 通过id删除订单
     * @param id
     */
    @Delete("delete from `order` where id = #{id}")
    void deleteById(int id);

    /**
     * 通过id查询订单信息
     * @param id
     */
    @Select("select id, order_id, dist_id from `order` where id = #{id}")
    @Result(column = "order_id", property = "orderId")
    @Result(column = "dist_id", property = "distId")
    OrderSimpleOrderIdAndDistIdVO selectById(int id);

    /**
     * 通过id查询“订单号”
     * @param id
     * @return
     */
    @Select("select order_id from `order` where id = #{id}")
    String selectOrderIdById(int id);

    /**
     * 查询订单的“订单号和用户id”
     * @param id 订单id
     * @return
     */
    @Select("select order_id, user_id from `order` where id = #{id}")
    @Result(column = "order_id", property = "orderId")
    @Result(column = "user_id", property = "userId")
    OrderSimpleOrderIdAndUserIdVO selectSimpleById(int id);

    /**
     * 通过“用户id”查询订单
     * @param orderPagerDTO
     * @return
     */
    @Select(
            "select id, order_id, state, created, freight, dist_id, total from `order` " +
                    "where user_id = #{userId} " +
                    "order by created desc " +
                    "limit #{offset}, #{size}"
    )
    @Result(column = "order_id", property = "orderId")
    @Result(
            column = "order_id", property = "subOrders",
            many = @Many(select = "cn.tedu.fresheveryday.mapper.SubOrderMapper.selectDetailsByOrderId")
    )
    List<OrderListVO> selectByUserId(OrderPagerDTO orderPagerDTO);

    /**
     * 通过“用户id”查询订单
     * @param userId 用户id
     * @return
     */
    @Select("select count(*) from `order` where user_id = #{userId} order by created desc")
    int selectCountByUserId(String userId);

    /**
     * 通过“订单状态和用户id”查询订单信息
     * @param orderPagerDTO
     * @return
     */
    @Select(
            "select id, order_id, state, created, freight, dist_id, total from `order` " +
                    "where state = #{state} and user_id = #{userId} " +
                    "order by created desc " +
                    "limit #{offset}, #{size}"
    )
    @Result(column = "order_id", property = "orderId")
    @Result(
            column = "order_id", property = "subOrders",
            many = @Many(select = "cn.tedu.fresheveryday.mapper.SubOrderMapper.selectDetailsByOrderId")
    )
    List<OrderListVO> selectByStateAndUserId(OrderPagerDTO orderPagerDTO);

    /**
     * 通过“订单状态和用户id”查询订单总数
     * @param state
     * @param userId
     * @return
     */
    @Select(
            "select count(*) from `order` " +
                    "where state = #{state} and user_id = #{userId} " +
                    "order by created desc"
    )
    int selectCountByStateAndUserId(@Param("state") int state, @Param("userId") String userId);

    /**
     * 通过“订单号”查询订单
     * @param orderId
     * @return
     */
    @Select("select id, order_id from `order` where order_id = #{orderId}")
    Order selectByOrderId(String orderId);

    /**
     * 通过“配送id”查询订单
     *
     * @param distId 配送信息id
     * @return
     */
    @Select("select id, order_id, update_times from `order` where dist_id = #{distId}")
    @Result(column = "id", property = "id")
    @Result(column = "order_id", property = "orderId")
    @Result(column = "update_times", property = "updateTimes")
    OrderSimpleVO selectByDistId(String distId);

    /**
     * 通过id修改订单的“状态”
     * @param id
     * @param state
     */
    @Update("update `order` set state = #{state} where id = #{id}")
    void updateStateById(@Param("id") int id, @Param("state") int state);

    /**
     * 通过id修改订单的“修改次数”
     * @param id          订单id
     * @param updateTimes 修改次数
     */
    @Update("update `order` set update_times = #{updateTimes} where id = #{id}")
    void updateUpdateTimesById(@Param("id") Integer id, @Param("updateTimes") int updateTimes);
}